package xyz.xmh.pattern.structural.filterpattern;

import java.util.ArrayList;
import java.util.List;

/**
 * FilterPatternDemo
 * 过滤器模式（又叫标准模式）演示<br/>
 * 这个模式是将一组数据，经过一个或者一系列的标准（过滤）
 *
 * @author xuMingHai
 * @since 2021/4/30 星期五 14:41
 */
public class FilterPatternDemo {

    public static void main(String[] args) {
        List<Person> personList = new ArrayList<>();

        personList.add(new Person("Robert", "Male", "Single"));
        personList.add(new Person("John", "Male", "Married"));
        personList.add(new Person("Laura", "Female", "Married"));
        personList.add(new Person("Diana", "Female", "Single"));
        personList.add(new Person("Mike", "Male", "Single"));
        personList.add(new Person("张三", "Male", "Married"));
        personList.add(new Person("李四", "Female", "Single"));
        personList.add(new Person("王五", "Female", "Married"));

        System.out.println("源数据：");
        printPersonList(personList);

        // 男性过滤器
        Criteria male = new CriteriaMale();
        // 女性过滤器
        Criteria female = new CriteriaFemale();
        // 单身过滤器
        Criteria single = new CriteriaSingle();
        // 单身女性过滤器
        Criteria femaleAndSingle = new AndCriteria(female, single);
        // 单身或是女性过滤器
        Criteria femaleOrSingle = new OrCriteria(female, single);


        // 过滤为男性的
        System.out.println("\nMALE:");
        printPersonList(male.meetCriteria(personList));

        // 过滤为女性的
        System.out.println("\nFEMALE:");
        printPersonList(female.meetCriteria(personList));

        // 过滤为单身的女性
        System.out.println("\nFEMALE_AND_SINGLE:");
        printPersonList(femaleAndSingle.meetCriteria(personList));

        // 过滤为单身或是女性
        System.out.println("\nFEMALE_OR_SINGLE:");
        printPersonList(femaleOrSingle.meetCriteria(personList));


    }

    /**
     * 打印集合
     *
     * @param personList 集合
     */
    private static void printPersonList(List<Person> personList) {
        for (Person person : personList) {
            System.out.println("Person : [ Name : " + person.getName()
                    + ", Gender : " + person.getGender()
                    + ", Marital Status : " + person.getMaritalStatus()
                    + " ]");
        }

    }
}
